$(document).ready(function () {
    Date.prototype.pattern = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        var week = {
            "0": "/u65e5",
            "1": "/u4e00",
            "2": "/u4e8c",
            "3": "/u4e09",
            "4": "/u56db",
            "5": "/u4e94",
            "6": "/u516d"
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        if (/(E+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        var str = fmt.lastIndexOf("N");
        if (str != -1) {
            return "";
        }

        return fmt;
    }
    /* var msg = $("#request_msg").val();
     if(msg == "ERROE"){
     swal("获取数据异常", "请联系管理员处理", "error");
     }*/
    /**********************初始化函数***********************/
    function Init() {
        var orderId = $("#orderId").val();

        // 1.加载门店
        $.ajax({
            url: 'getStoreList.action',//
            data: "",
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                var dataL = data.data;
                var bool = data.ret;
                var list = '';
                $("#store_list").empty();
                var options = '<option value="0">全部</option>';
                if (bool == 0 || bool == '0') {
                    for (var i = 0; i < dataL.length; i++) {
                        var datas = dataL[i];
                        options = options + '<option value="'
                            + datas.code + '">'
                            + datas.name + '</option>';
                    }

                }
                $("#store_list").append(options);
                $("#store_list").trigger("chosen:updated");
                $("#store_list").chosen();
            },
            error: function () {
                swal("获取数据失败", "请联系管理员处理", "error");
            }
        });

        $.ajax({
            url: 'courseRegList.action',//
            data: {
                orderId: orderId
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (datas) {
                var html = '';
                var data = datas.data.list;
                var num = datas.data.num;
                var course_num = $("#course_num").val();
                /**/
                if (num >= course_num) {
                    $("#div_courseAdd").hide();
                }
                debugger;
                $("#courselist").empty();
                var text = '课时' + (num + 1) + '：<span>未销课</span>';
                $("#keshi").html(text);
                for (var i = 0; i < num; i++) {
                    html = html +
                        '<div class="form-group margin-top"><label class="col-sm-12">课时' + (i + 1) + '：<span>已销课</span></label></div>' +
                        '<div class="hr-line-dashed"></div>' +
                        '<div class="form-group"><label class="col-sm-2 control-label">上课时间</label>' +
                        '<div class="col-sm-10">' +
                        '<div class="input-group date" style="max-width:300px;">' +
                        '<span class="input-group-addon"><i class="fa fa-calendar"></i></span>' +
                        '<input type="text" class="form-control" value="' + new Date(data[i].courseTime).pattern("yyyy-MM-dd hh:mm:ss") + '" disabled>' +
                        '</div>' +
                        '</div>' +
                        '</div>' +
                        '<div class="hr-line-dashed"></div>' +
                        '<div class="form-group"><label class="col-sm-2 control-label">上课门店</label>' +
                        '<div class="col-sm-10" style="max-width:300px;" disabled>' +
                        '<input class="form-control" id="daterange" type="text" name="daterange" value="' + data[i].storeName + '" style="width: 280px" disabled/>' +
                        '</div>' +
                        '</div>' +
                        '<div class="hr-line-dashed"></div>';
                }
                $("#courselist").append(html);
            },
            error: function () {
                swal("页面数据加载失败", "", "error");
            }
        });


    }

    var msg = $("#request_msg").val();
    if (msg == "ERROE") {
        swal("获取数据异常", "请联系管理员处理", "error");
    } else {
        Init();
    }

    // 点击销课的时候 提交数据
    $("#btn_save").click(function () {

        var courseTime = $("#course_time").val();
        var store_id = $("#store_list").val();
        var orderId = $("#orderId").val();
        var str = courseTime.split("/");
        courseTime = str[2] + '-' + str[1] + '-' + str[0];
        $.ajax({
            url: 'saveCourseReg.action',//
            data: {
                "orderId": orderId,
                "courseTime": courseTime,
                "storeId": store_id
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (datas) {
                swal("销课！", datas.msg, datas.data);
                window.location.reload();
            },
            error: function () {
                swal("销课失败", "", "error");
            }
        });

    });
});